﻿using System.Collections.ObjectModel;
using Unknown.Framework.Database;
using Unknown.Framework.Injection;
using Unknown.Source.Entity.NormalView;
using Unknown.Source.Sql.NormalView;

namespace Unknown.Source.Access.NormalView
{
    /// <summary>
    /// 表访问抽象类
    /// </summary>
    public abstract class CascadeTableAccess
    {
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="first">开始行</param>
        /// <param name="last">结束行</param>
        /// <returns>结果</returns>
        public virtual Collection<CascadeTable> ExecuteSelect(int first, int last)
        {
            Collection<CascadeTable> results = new Collection<CascadeTable>();

            DatabaseHandler databaseHandler = new DatabaseHandler();
            ICascadeTableSqlCustom sql = ObjectFactory.Build(typeof(ICascadeTableSqlCustom)) as ICascadeTableSqlCustom;
            if (sql != null)
            {
                Operation operation = sql.BuildSelect(first, last);
                if (operation != null)
                {
                    AbstractReaderOperationHandler<CascadeTable> operationHandler = operation.OperationHandler as AbstractReaderOperationHandler<CascadeTable>;
                    if (operationHandler != null)
                    {
                        databaseHandler.Execute(operation);
                        results = operationHandler.Results;
                    }
                }
            }

            return results;
        }
    }
}
